package com.yycx.module.bus.provider.handler.haiying.salemanbalance.tasktodo;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSalemanBank;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusSalemanBankService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 退保证金单保存
 *
 * @author zyf
 */
@Component
public class RefundBondPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private OrderNoUtil orderNoUtil;

    @Resource
    private HyOrderDetailService orderDetailsService;

    @Resource
    private BusSalemanBankService salemanBankService;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String selectIds = cs.getParams("selectIds");
        Long bankId = cs.getLong("bankId");
        ApiAssert.isNotEmpty("请选择订单明细", selectIds);
        EntityMap refundBondMap = orderDetailsService.totalRefundBondInfo(selectIds);
        Integer count = refundBondMap.getInt("count");
        if (count > 1) {
            ApiAssert.failure("招商和自营类型商品不能同时申请");
        }
        BusSalemanBank saleManBank=salemanBankService.getById(bankId);
        BigDecimal customBondTakeAmount = refundBondMap.getBigDecimal("customBondTakeAmount");
        Long saleManId=refundBondMap.getLong("saleManId");
        Long deptId=refundBondMap.getLong("deptId");
        String drugType = refundBondMap.get("drugType");
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setCompanyId(OpenHelper.getCompanyId());
        payment.setAmount(customBondTakeAmount);
        payment.setSaleManId(saleManId);
        payment.setDeptId(deptId);
        payment.setDrugType(drugType);
        payment.setBankNo(saleManBank.getBankNo());
        payment.setBankName(saleManBank.getBankName());
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setTitle("退保证金支付申请单");
        payment.setAmountType(AmountTypeEnum.CbReturn.getCode());
        String orderNo = orderNoUtil.getCode("TK");
        payment.setPaymentNo(orderNo);
        payment.setSelectIds(selectIds);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String selectIds = cs.getParams("selectIds");
        //关联退保金申请ID
        orderDetailsService.updateRefundBondId(entity.getPaymentId(), selectIds);
    }

}
